import { _decorator, AudioClip, AudioSource, Component, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('MultipleAudioControler')
export class MultipleAudioControler extends Component {
	@property(AudioClip)
	clips: AudioClip[] = [];
	audioSource!: AudioSource;
	protected onLoad(): void {
		this.audioSource = this.node.addComponent(AudioSource);
		this.audioSource.loop = false;
		this.audioSource.playOnAwake = false;
		this.audioSource.volume = 1;
	}
	start() {}

	play(clipName: string, loop: boolean = false) {
		let clip = this.clips.find((item) => item.name === clipName);
		if (!clip) return;
		this.audioSource.clip = clip;
		if (loop) {
			this.audioSource.loop = loop;
			this.audioSource.play();
		} else {
			this.audioSource.playOneShot(clip, 1);
		}
	}
	stop() {
		this.audioSource.stop();
	}
	pause() {
		this.audioSource.pause();
	}
	update(deltaTime: number) {}
}
